jQuery(function($) {

    $('.selectJiraIssueTypeAnchor').click(function(event) {
        var anchorId = event.target.id;
        acproKeyKeeper = anchorId.substring("selectJiraIssueTypeAnchor".length);
        $.getJSON(jiraIssueTypesUrl, function(data) {
            dataKeeper = data.issueTypes;
            buildDialog("issueTypesDialog", "issueTypesDialogTable", "anchorJiraIssueTypeId", "anchorJiraIssueTypeId",
                    "tdJiraIssueTypeId", "tdJiraIssueTypeName", "issueTypeMapping");
            $('#issueTypesDialog').dialog('open');
        });
        return false;
    });

    $('.anchorDefaultJiraIssueType').click(function(event) {
        acproKeyKeeper = ""; //hack
        $.getJSON(jiraIssueTypesUrl, function(data) {
            dataKeeper = data.issueTypes;
            buildDialog("issueTypesDialog", "issueTypesDialogTable", "anchorDefaultJiraIssueType", "anchorDefaultJiraIssueType",
                    "tdDefaultJiraIssueTypeId", "tdDefaultJiraIssueTypeName", "defaultJiraIssueTypeId");
            $('#issueTypesDialog').dialog('open');
        });
        return false;
    });

    $('.selectJiraPriorityAnchor').click(function(event) {
        var anchorId = event.target.id;
        acproKeyKeeper = anchorId.substring("selectJiraPriorityAnchor".length);
        $.getJSON(jiraPrioritiesUrl, function(data) {
            dataKeeper = data.priorities;
            buildDialog("prioritiesDialog", "prioritiesDialogTable", "anchorJiraPriorityId", "anchorJiraPriorityId",
                    "tdJiraPriorityId", "tdJiraPriorityName", "priorityMapping");
            $('#prioritiesDialog').dialog('open');
        });
        return false;
    });

    $('.selectJiraStatusAnchor').click(function(event) {
        var anchorId = event.target.id;
        acproKeyKeeper = anchorId.substring("selectJiraStatusAnchor".length);
        $.getJSON(jiraStatusesUrl, function(data) {
            dataKeeper = data.statuses;
            buildDialog("statusesDialog", "statusesDialogTable", "anchorJiraStatusId", "anchorJiraStatusId",
                    "tdJiraStatusId", "tdJiraStatusName", "statusMapping");
            $('#statusesDialog').dialog('open');
        });
        return false;
    });

    $('.selectJiraResolutionAnchor').click(function(event) {
        var anchorId = event.target.id;
        acproKeyKeeper = anchorId.substring("selectJiraResolutionAnchor".length);
        $.getJSON(jiraResolutionsUrl, function(data) {
            dataKeeper = data.resolutions;
            buildDialog("resolutionDialog", "resolutionDialogTable", "anchorJiraResolutionId", "anchorJiraResolutionId",
                    "tdJiraResolutionId", "tdJiraResolutionName", "resolutionMapping");
            $('#resolutionDialog').dialog('open');
        });
        return false;
    });

    $('.selectJiraProjectVersionAnchor').click(function(event) {
        var anchorId = event.target.id;
        acproKeyKeeper = anchorId.substring("selectJiraProjectVersionAnchor".length);
        $.getJSON(jiraProjectVersionsUrl, function(data) {
            dataKeeper = data.projectVersions;
            buildDialog("projectVersionDialog", "projectVersionDialogTable", "anchorJiraProjectVersionId", "anchorJiraProjectVersionId",
                    "tdJiraProjectVersionId", "tdJiraProjectVersionName", "projectVersionMapping");
            $('#projectVersionDialog').dialog('open');
        });
        return false;
    });

    var acproKeyKeeper;
    var dataKeeper;

    function buildDialog(dialogId, tableId, dialogAnchorClass, dialogAnchorId, tdJiraObjId, tdJiraObjName, hiddenId) {
        var dataArray = dataKeeper; 
        $("#" + tableId + " tr:not(:first)").remove();
        var content = "";
        for (var i = 0; i < dataArray.length; i++) {
            var jiraObj = dataArray[i];
            content += "<tr>";
            content += "<td>" + jiraObj.id + "</td>";
            content += "<td><a href='#' class='" + dialogAnchorClass + "' id='" + dialogAnchorId + "" + i + "'>" + jiraObj.name + "</a></td>";
            content += "</tr>";
        }
        $("#" + tableId).append(content);
        $('.' + dialogAnchorClass).click(function(event) {
            var anchorId = event.target.id;
            var index = anchorId.substring(dialogAnchorClass.length);
            var jiraObj = dataArray[index];

            $("#" + tdJiraObjId + acproKeyKeeper).text(jiraObj.id);
            $("#" + tdJiraObjName + acproKeyKeeper).text(jiraObj.name);

            $("#" + hiddenId + acproKeyKeeper).val(jiraObj.id);

            $('#' + dialogId).dialog('close');
            return false;
        });
    }

    var dialogOpts = {
        position: ['center', 'middle'],
        autoOpen: false,
        height: 500,
        width: 500,
        resizable: false,
        modal: true,
        closeText: '',
        buttons: {
            'Cancel': function() {
                $(this).dialog("close");
             }
        }
    };

    var dialogResolutionOpts = {
        position: ['center', 'middle'],
        autoOpen: false,
        height: 500,
        width: 500,
        resizable: false,
        modal: true,
        closeText: '',
        buttons: {
            'Cancel': function() {
                $(this).dialog("close");
             },
            'No resolution (Unresolved)': function() {
                $("#tdJiraResolutionId" + acproKeyKeeper).text("");
                $("#tdJiraResolutionName" + acproKeyKeeper).text("");
                $("#resolutionMapping" + acproKeyKeeper).val("");

                $(this).dialog("close");
             }
        }
    };
    $('#issueTypesDialog').dialog(dialogOpts);

    $('#prioritiesDialog').dialog(dialogOpts);

    $('#statusesDialog').dialog(dialogOpts);

    $('#resolutionDialog').dialog(dialogResolutionOpts);

    $('#projectVersionDialog').dialog(dialogOpts);

});